<template>
    <div class="data_container">
        <div class="data_main">
            <!-- 主体头部 -->
            <div class="top_area">
                <i class="el-icon-arrow-left"></i>
                <span class="return_operation" @click="returnOperation">返回</span>
                <span class="leftVertical_ine"></span>
                <span class="data_name">数据包：</span>
                <span class="data_name">{{packetDataName}}</span>
                <span style="display: inline-block; float:right">
                    <el-button type="primary" plain  @click="archiveOperation" v-if="storeTpyeBtn">修改存储类别</el-button>
                    <el-button 
                        :type="storeTpyeBtn ? 'info' : 'primary'" plain  
                        @click="retrievesOperationDialogVisible = true" 
                        v-if="recoveryBtn"
                        >恢复
                    </el-button>
                    <el-button type="info" plain @click="saveData">保存</el-button>
                    <el-button type="info" plain @click="deleteData">删除</el-button>
                    <el-button type="info" plain @click="visualization">可视化预览</el-button>
                </span>
            </div>

            <!-- 主体区域 -->
            <div class="main_area">
                <ul>
                    <li>
                        <infoComponentVue 
                            info-title="基本信息"
                            :info-data="baseInfoData"
                            :info-data2="baseInfoData2"
                            isDescriptionTextAreaShow = "true"
                            isTagShow = "true"
                            :descri = "pageDataSet.remark"
                            :tag = "tag"
                            ref = "baseInfo"
                        ></infoComponentVue>
                    </li>
                    <li>
                        <infoComponentVue 
                            info-title="车辆信息"
                            :info-data="carInfoData"
                            ref = "carInfo"
                        ></infoComponentVue>
                    </li>
                    <li>
                        <infoComponentVue 
                            info-title="存储信息"
                            :info-data="storeInfoData"
                            ref = "storeInfo"
                        ></infoComponentVue>
                    </li>
                </ul>
            </div>
            <div class="main_record" v-if="propid">
              <el-tabs v-model="activeName" style="margin-right:1.25rem;">
                 <el-tab-pane label="提取记录" name="first"><extract :id="propid"/></el-tab-pane>
                 <el-tab-pane label="下载记录" name="second"><download :id="propid"/></el-tab-pane>
             </el-tabs>
             <div class="export">导出</div>
            </div>
        </div>

        <!-- 恢复操作 -->
        <el-dialog
            title="恢复"
            :visible.sync="retrievesOperationDialogVisible"
            width="25%"
            :show-close="false"
            >
            <div class="term_section">
                <span>有效期</span>
                <span style="margin: 0px 0.625rem 0 4rem;">
                    <el-input-number v-model="dayNum" :min="1" :max="30"></el-input-number>
                </span>
                <span class="term_day">天</span>
                <div style="paddingLeft: 6.25rem;paddingTop: 0.625rem">有效期限制为1~30天</div>
            </div>
            <div class="rate_section">
                <span class="rate">速率</span>
                <el-radio-group v-model="speedOptions">
                    <el-radio label="EXPEDITED">加急</el-radio>
                    <el-radio label="STANDARD">标准</el-radio>
                </el-radio-group>
                <p v-if="speedOptions === 'EXPEDITED'">可在3~5分钟内恢复以上对象。</p>
                <p v-if="speedOptions === 'STANDARD'">可在3~5小时内恢复以上对象。</p>
            </div>
            <span slot="footer">
                <el-button size="small" type="primary" plain @click="retrievesOperationConfirm">确 定</el-button>
                <el-button size="small" type="info" plain @click="retrievesOperationDialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>

        <!-- 归档操作 -->
        <el-dialog
            class="dialog"
            title="修改存储类别"
            :visible.sync="archiveOperationDialogVisible"
            width="25%"
            :show-close="false"
            :before-close="batchReviseCategoryCancel"
            >
            <div class="storage_section">
                <span class="store">存储类别</span>
                <el-radio-group v-model="storeOptions">
                    <el-radio :label = 0 :disabled="standardRadio">标准存储</el-radio>
                    <el-radio :label = 1 :disabled="lowerRadio">低频访问存储</el-radio>
                    <el-radio :label = 2 :disabled="fileRadio">归档存储</el-radio>
                </el-radio-group>
                <p v-if="storeOptions == 0">适用于有大量热点文件或小文件，且需要频繁访问（平均一个月多次）并快速获取数据的业务场景。</p>
                <p v-if="storeOptions == 1">适用于不频繁访问（平均一年少于12次），但快速获取数据的业务场景。</p>
                <p v-if="storeOptions == 2">适用于很少访问（平均一年一次）且对数据获取速率要求不高的业务场景。</p>
            </div>
            <span slot="footer" class="store_footer">
                <el-button size="small" type="primary" plain @click="batchReviseCategoryConfirm">确 定</el-button>
                <el-button size="small" type="info" plain @click="batchReviseCategoryCancel">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import infoComponentVue from '../../../../components/infoComponent/infoComponent.vue'
import extract from '../../../../components/infoComponent/extractRecords.vue'
import download from '../../../../components/infoComponent/downloadRecord.vue'
export default {
    components: {
        infoComponentVue,extract,download
    },
    data() {
        return {
            pageDataSet: {},
            retrievesOperationDialogVisible: false,
            archiveOperationDialogVisible: false,
            dayNum: 1,
            speedOptions: 'EXPEDITED',
            packetDataName: '',
            storeOptions: 0,
            standardRadio: false,
            lowerRadio: false,
            fileRadio: false,
            baseInfoData: [],
            baseInfoData2: [],
            carInfoData: [],
            storeInfoData: [],
            recoveryStatus:['--','未恢复','恢复中','已恢复'],
            storageStatus:['标准存储','低频存储','归档存储'],
            tag: [],
            activeName: 'first',
        }
    },
    
    methods: {  
        // 返回上一级
        returnOperation() {
            this.$router.push({path:'/AnonymizeData'})
        },
        formatBytes(a, b) { 
            if (a == 0) return '0 B'; 
            var c = 1024, d = b, e = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], f = Math.floor(Math.log(a) / Math.log(c)); 
            return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f];
        },
        // 顶部恢复按钮事件
        retrievesOperation() {
            this.retrievesOperationDialogVisible = true
        },
        // 恢复弹框确认事件
        retrievesOperationConfirm() {
            this.$url.post('data/anonymized/restore', {
                'ids': [this.pageDataSet.id],
                'days_num': this.dayNum,
                'rate': this.speedOptions
            })
            .then(res => {
                if (res.status === 200) {    
                    this.getData()                   
                    this.$message.success('恢复成功！')
                } else {
                    this.$message.error('恢复失败！')
                }
            })
            this.retrievesOperationDialogVisible = false;
        },
        // 点击修改存储类型事件
        archiveOperation() {        
            if (this.pageDataSet.storage_type == 0) {
                this.standardRadio = true;
                this.lowerRadio = false;
                this.fileRadio = false;
                this.storeOptions = 1;
            }
            else if (this.pageDataSet.storage_type == 1) {
                this.standardRadio = false;
                this.lowerRadio = true;
                this.fileRadio = false;
                this.storeOptions = 0;
            }
            else {
                if (this.pageDataSet.restoration_status == 3) {
                    this.standardRadio = false;
                    this.lowerRadio = false;
                    this.fileRadio = true;
                    this.storeOptions = 0;
                }
            }
            this.archiveOperationDialogVisible = true;
        },
        // 确认修改存储类型事件
        batchReviseCategoryConfirm() {
            this.$url.post('data/anonymized/storageclass', {
                'ids': [this.pageDataSet.id],
                'storage_class': this.storeOptions
            })
            .then(res=>{
                if (res.status === 200) {
                    this.getData();
                    this.$message.success('修改成功')                        
                } else {
                    this.$message.error('修改失败！')
                }
                this.archiveOperationDialogVisible = false;
            })
            .catch(res=>{
                this.archiveOperationDialogVisible = false;
            })
            this.storeOptions = 0
        },
        // 取消修改存储类型事件
        batchReviseCategoryCancel() {
            this.archiveOperationDialogVisible = false;
            this.storeOptions = 0
        },
        // 点击保存按钮事件
        saveData(){
            let newTagList = JSON.parse(JSON.stringify(this.$refs.baseInfo.tagList));  
            this.$url.put('data/anonymized/'+ this.pageDataSet.id, {
                'remark': this.$refs.baseInfo.description,
                'tag': newTagList,
            })
            .then(res => {
                if (res.status === 201) {                     
                    this.$message.success('保存成功')
                    this.pageDataSet.remark = this.$refs.baseInfo.description;
                    this.pageDataSet.tag = newTagList.join('|');
                    this.tag = newTagList;
                    window.sessionStorage.setItem('tempPageData',JSON.stringify(this.pageDataSet));
                } else {
                    this.$message.error('保存失败!')
                }
            })
        },
        // 点击删除按钮事件
        deleteData(){
            this.$confirm('确定删除此数据？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$url.delete('data/anonymized/'+ this.pageDataSet.id)
                .then(res => {
                    if (res.status === 204) {
                        this.$message.success('删除数据成功')
                        this.returnOperation();
                    } else {
                        this.$message.error('删除失败')
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        // 可视化预览
        visualization(){
           this.$url.get(`data/anonymized/visualization/${this.pageDataSet.id}`)  
            .then(res => {
                if (res.data) {
                    window.open(res.data.data,'_blank')
                } else {
                    this.$message.error('请求失败')
                }
            })
            
        },
       
        // 数据更新
        getData(){
            this.$url.get('data/anonymized/'+ this.pageDataSet.id)
            .then(res=>{
                if (res.data.data) {
                    this.pageDataSet = res.data.data;
                    window.sessionStorage.setItem('tempPageData',JSON.stringify(this.pageDataSet));
                    this.setStoreInfoComponent();
                    this.setBaseInfoComponent();
                }
            })
        },
        // 设置基本信息
        setBaseInfoComponent() {
            this.baseInfoData = [{
                label:'id',
                value:this.pageDataSet.id,     
            },{
                label:'名称',
                value:this.pageDataSet.name
            }];
            this.baseInfoData2 = [{label:'区域',value:this.pageDataSet.region}]
        },
        // 设置车辆信息
        setCarInfoComponent() {
            this.carInfoData = [{
                label:'车辆编号',
                value: this.pageDataSet.car_id
            },{
                label:'采集时间',
                value: this.pageDataSet.collect_time,
                isTime: true 
            },{
                label:'安全员',
                value: this.pageDataSet.user_safety,                
            },{
                label:'采集员',
                value: this.pageDataSet.user_collect,
            }]
        },
        // 设置存储信息
        setStoreInfoComponent() {
            this.storeInfoData = [{
                label:'版本号',
                value: this.pageDataSet.version,                
            },{
                label:'存储类别',
                value: this.storageStatus[this.pageDataSet.storage_type], 
            },{
                label:'恢复状态',
                value: this.recoveryStatus[this.pageDataSet.restoration_status], 
            },{
                label:'大小',
                value: this.formatBytes(this.pageDataSet.size,0), 
            },{
                label:'最后修改时间',
                value: this.pageDataSet.update_time,
                isTime: true 
            },{
                label:'链接',
                value: this.pageDataSet.url, 
            }];
        }
    },
    created() {
        let tempPageData = window.sessionStorage.getItem('tempPageData');
        if (tempPageData) {
            this.pageDataSet = JSON.parse(tempPageData);
        } else {
            this.pageDataSet = this.$route.params.data;
            window.sessionStorage.setItem('tempPageData', JSON.stringify(this.pageDataSet));
        }
        this.packetDataName = this.pageDataSet.name
        this.tag = this.pageDataSet.tag.split('|');
        this.setBaseInfoComponent();
        this.setCarInfoComponent();
        this.setStoreInfoComponent();
    },
    beforeDestroy(){
        window.sessionStorage.removeItem('tempPageData');
    },
    computed:{
        storeTpyeBtn(){
            return this.pageDataSet.restoration_status !== 1 && this.pageDataSet.restoration_status !== 2;
        },
        recoveryBtn(){
            return this.pageDataSet.restoration_status == 1 || this.pageDataSet.restoration_status == 3;
        },
        propid(){
            return this.pageDataSet.id? this.pageDataSet.id: this.propid;
        }
    }
}
</script>


<style lang="less">
.data_container {
    height: 100%;
    display: flex;
    margin: 0 30px 0 10px;
    // 主体区域样式
    .data_main {
        margin-top: 15px;
        // 主体顶部样式
        .top_area {
            margin-bottom: 25px;
            .el-icon-arrow-left {
                font-family: Microsoft YaHei, Microsoft YaHei-Normal;
                font-weight: Normal;
                font-size: 14px;
                color: #0a4cee;
            }
            .return_operation {
                display: inline-block;
                height: 30px;
                line-height: 30px;
                font-family: Microsoft YaHei, Microsoft YaHei-Normal;
                font-weight: Normal;
                font-size: 16px;
                color: #0a4cee;
                margin-right: 10px;
                cursor: pointer;
            }
            .leftVertical_ine {
                display: inline-block;
                height: 12px;
                border-left: 1px solid #d8d7d7;
                margin: 0 10px -2px 0px;
            }
            .data_name {
                font-family: Microsoft YaHei, Microsoft YaHei-Normal;
                font-weight: Normal;
                font-size: 14px;
                color: #131416;
            }
            .el-button {
                padding: 8px 37px;
                box-sizing: border-box;
            }
        }    
        .main_area {
            ul {
                padding: 0;
                margin: 0;
                list-style-type: none;
                display: flex;
                li {
                    margin-right: 20px;
                    border-radius: 2px;
                    box-sizing: border-box;
                    word-wrap:break-word;
                    white-space:normal;
                }
                li:last-child {
                    margin-right: 0px;
                }
            }
        }
    }
    // 提交下载记录
    .main_record{
        display: flex;
        position: relative;
        width: 1520px;
        height: 378px;
        padding-top: 20px; 
        .export{
            width: 100px;
            height: 34px;
            border: 1px solid #E6E6E6;
            font-size: 14px;
            line-height: 40px;
            text-align: center;
            position: absolute;
            left:1400px;
        }
        .el-tabs__item.is-top{
            color: #808080;
        }
        .el-tabs__nav.is-top{
            margin-left: 40px;
           
        }
        .el-tabs__item.is-top.is-active,.el-tabs__item.is-top{
            font-size: 16px;
            font-weight: 700;
            font-family: Microsoft YaHei;
        }
        .el-tabs__item.is-top.is-active,.el-tabs__item.is-top:hover{
            color: #333333;
        }
        .el-tabs__active-bar{
            background-color: #333333 !important;
           
        }
        .el-tabs__nav-wrap::after{
           height: 0px;
        }
    }
    
    
    // 恢复弹框样式
    .el-dialog__header {
        padding-left: 20px;
    }
    .el-dialog__body {
        padding-left: 32px;
    }
    .term_section {
        font-family: Microsoft YaHei, Microsoft YaHei-Normal;
        font-weight: Normal;
        font-size: 12px;
        color: #808080;
        margin-bottom: 20px;
        .el-input-number.is-without-controls .el-input__inner {  
            width: 100px;  
            line-height: 28px;  
            height: 28px;
        }
        .el-input__inner {
            height: 30px;
            box-sizing: border-box;
            border-radius: 2px;
            font-size: 12px;
        }
        .el-input-number {
            line-height: 28px;
        }
        .el-input-number__decrease {
            border-radius: 2px 0 0 2px;
            background-color: #fff;
            width: 28px
        }
        .el-input-number__increase {
            border-radius: 0 2px 2px 0;
            background-color: #fff;
            width: 28px
        }
        .term_day {
            font-family: Microsoft YaHei, Microsoft YaHei-Normal;
            font-weight: Normal;
            font-size: 12px;
            color: #131416;
        }
    }
    .rate_section {
        margin-bottom: 25px;
        .rate {
            font-family: Microsoft YaHei, Microsoft YaHei-Normal;
            font-weight: Normal;
            font-size: 12px;
            color: #808080;
            margin-right: 76px;
        }
        .el-radio__label {
            font-family: Microsoft YaHei, Microsoft YaHei-Normal;
            font-weight: Normal;
            font-size: 12px;
            color: #131416;
        }
        p {
            font-family: Microsoft YaHei, Microsoft YaHei-Normal;
            font-weight: Normal;
            font-size: 12px;
            color: #808080;
            margin: 20px 0 5px 100px;
        }
    }

    // 归档弹框样式
    .el-dialog__title {
        font-family: Microsoft YaHei, Microsoft YaHei-Bold;
        font-weight: Bold;
        font-size: 16px;
        color: #131416;
    }
    .el-dialog__body {
        padding: 5px 20px;
        .el-radio__label {
            font-family: Microsoft YaHei, Microsoft YaHei-Normal;
            font-weight: Normal;
            font-size: 12px;
            color: #131416;
        }
        .storage_section {
            margin-bottom: 35px;
            .store {
                font-family: Microsoft YaHei, Microsoft YaHei-Normal;
                font-weight: Normal;
                font-size: 12px;
                color: #808080;
                margin-right: 50px;
            }
            p {
                font-family: Microsoft YaHei, Microsoft YaHei-Normal;
                font-weight: Normal;
                font-size: 12px;
                color: #808080;
                margin: 20px 45px 0px 100px; 
            }
        }
    }
    .el-dialog__footer {
        text-align: center;
        padding: 0px 20px 20px;
        .el-button--small, .el-button--small.is-round {
            padding: 8px 85px;
            box-sizing: border-box;
        }
    }
    //按钮样式
    .el-button.el-button--info.is-plain,.el-button.el-button--primary.is-plain {
        border-color: #e6e6e6;
        border-radius: 1px;
        font-size: 12px;
        font-family: Microsoft YaHei, Microsoft YaHei-Normal;
        font-weight: Normal;
        text-align: CENTER;
        color: #131416;
        color: #15141C;
        background-color: #fff;

        &:disabled {
            background-color: #F8F8F8;
            border: 1px solid #E6E6E6;
            color: #A1A1A1;

            &:hover {
                background-color: #F8F8F8;
                border: 1px solid #E6E6E6;
                color: #A1A1A1;
                box-shadow: none;
            }
        }
    }
    .el-button.el-button--primary.is-plain {
        background-color: #0a4cee;
        border-color: #0a4cee;
        color: #fff;
        &:hover {
            box-shadow: 0 2px 8px 0 rgba(10, 76, 238, .5);
        }
    }
    .el-button.el-button--info.is-plain:hover {
        font-family: Microsoft YaHei, Microsoft YaHei-Normal;
        font-weight: Normal;
        font-size: 12px;
        box-shadow: 0 2px 8px 0 rgba(230, 230, 230, .5);
    }

    .el-button--info.is-plain:focus,
    .el-button--info.is-plain:hover {
        background: none;
    }
    // 修改Radio单选框样式
    .el-radio__inner:hover {
        border-color: #0a4cee !important;
    }
    
    .el-radio__input.is-checked .el-radio__inner {
        background-color: #fff !important;
        border-color: #0a4cee !important;
    }
    
    .el-radio__inner::after {
        width: 8px;
        height: 8px;
        background-color: #0a4cee;
    }
}
</style>